﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DestinationOperations;

namespace Beginning
{
    public partial class frmDestinationDetails : Form
    {
        public long id
        {
            get;
            set;
        }

        public frmDestinationDetails(long id)
        {
            InitializeComponent();
            this.id = id;
            StartHoursBindControls();
            BindControls();
            ConcreteBindControls();
            ShowConcreteDetailsBindControls();
        }

        private void StartHoursBindControls()
        {
            List<string> startHour = new List<string>();
            List<int> startHoursList = DAO.GetAllStartHoursForDestination(id);

            foreach (var sh in startHoursList)
            {
                startHour.Add((sh / 60).ToString() + ":" + (sh % 60).ToString());
            }

            cbStartHours.DataSource = startHour;
        }

        private string CalculateTheCompleteTime(string hours, int times)
        {
            int result;
            string[] time = hours.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
            result = (Convert.ToInt32(time[0]) * 60) + Convert.ToInt32(time[1]) + times;

            if ((result / 60) < 24)
            {
                return ((result / 60).ToString() + ":" + (result % 60).ToString());
            }
            else
            {
                return (((result / 60) - 24).ToString() + ":" + (result % 60).ToString());
            }
        }

        private string CalculateTheConcreteTime(int time)
        {
            return ((time / 60).ToString() + "h " + (time % 60).ToString() + "min");
        }

        private void BindControls()
        {
            var data = from w in DAO.GetAllWayStationForDestination(id)
                       orderby w.WayStationOrderNumber
                       select new
                       {
                           ID = w.WayStationID,
                           Settlement = w.Settlement.SettlementName,
                           Price = w.WayStationPriceTo.ToString(),
                           DistanceTo = w.WayStationDistanceTo.ToString(),
                           TimeTo = (w.WayStationTimeTo / 60).ToString() + "h " + (w.WayStationTimeTo % 60).ToString() + "min",
                           Stay = w.WayStationStay,
                           Hour = CalculateTheCompleteTime(cbStartHours.Text, DAO.GetWayStationTimesFromBeginning(id, w.WayStationOrderNumber))
                       };

            dgvDestinationDetails.DataSource = data.ToList();
        }

        private void ConcreteBindControls()
        {
            string lastWayStation = DAO.GetTheLastWayStationForDestination(id).Settlement.SettlementName;

            var dataFrom = from d in DAO.GetAllWayStationForDestination(id)
                           where d.Settlement.SettlementName != lastWayStation
                           select d.Settlement.SettlementName;

            cbFrom.DataSource = dataFrom.ToList();            
        }

        private void ShowConcreteDetailsBindControls()
        {
            var fromOrderNumber = (from f in DAO.GetAllWayStationForDestination(id)
                                  where f.Settlement.SettlementName == cbFrom.Text
                                  select f.WayStationOrderNumber).First();

            var toOrderNumber = (from t in DAO.GetAllWayStationForDestination(id)
                                where t.Settlement.SettlementName == cbTo.Text
                                select t.WayStationOrderNumber).First();

            txtTime.Text = CalculateTheConcreteTime(DAO.GetTimeBetweenConcreteStations(id, fromOrderNumber, toOrderNumber));
            txtDistance.Text = DAO.GetDistancesBetweenConcreteStations(id, fromOrderNumber, toOrderNumber).ToString();
            txtPrice.Text = DAO.GetPricesBetweenConcreteStations(id, fromOrderNumber, toOrderNumber).ToString("F");
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            frmMain main = new frmMain();
            main.Show();
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            gbAddStartHour.Enabled = true;
        }

        private void btnCancelStartHour_Click(object sender, EventArgs e)
        {
            gbAddStartHour.Enabled = false;
        }

        private void btnInsertingStartHour_Click(object sender, EventArgs e)
        {
            StartHour startHour = new StartHour();
            startHour.DestinationID = id;
            startHour.StartHourValue = ((controlStartHour.Hours * 60) + controlStartHour.Minutes);
            startHour.SheduleTypeID = DAO.GetWeekScheduleForDestination(id);

            if (!DAO.IsStartHourExists(controlStartHour.Hours.ToString() + ":" + controlStartHour.Minutes.ToString()))
            {
                DestinationOperations.DAO.AddStartHour(startHour);
                MessageBox.Show("A new start hour added!");
            }
            else
            {
                MessageBox.Show("The start hour exists!");
            }

            StartHoursBindControls();
            gbAddStartHour.Enabled = false;
        }

        private void btnAddWayStation_Click(object sender, EventArgs e)
        {
            frmAddWayStation addWayStation = new frmAddWayStation(id);
            addWayStation.Show();
            this.Hide();
        }

        private void cbStartHours_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindControls();
        }

        private void btnEditWayStation_Click(object sender, EventArgs e)
        {
            DataGridViewCellCollection current;
            current = dgvDestinationDetails.CurrentRow.Cells;

            frmEditWayStation editWayStation = new frmEditWayStation(
                Convert.ToInt64(current["ID"].Value.ToString()), id);
            editWayStation.Show();
            this.Hide();
        }

        private void btnRemoveWayStation_Click(object sender, EventArgs e)
        {
            DataGridViewCellCollection current;
            current = dgvDestinationDetails.CurrentRow.Cells;

            frmDeleteWayStation editWayStation = new frmDeleteWayStation(
                Convert.ToInt64(current["ID"].Value.ToString()), id);
            editWayStation.Show();
            this.Hide();
        }

        private void btnConcreteDetails_Click(object sender, EventArgs e)
        {
            gbConcreteDetails.Enabled = true;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            gbConcreteDetails.Enabled = false;
        }

        private void cbFrom_SelectedIndexChanged(object sender, EventArgs e)
        {
            var fromOrderNumber = from n in DAO.GetAllWayStationForDestination(id)
                                  where n.Settlement.SettlementName == cbFrom.Text
                                  select n.WayStationOrderNumber;

            var dataTo = from d in DAO.GetAllWayStationForDestination(id)
                         where d.WayStationOrderNumber > fromOrderNumber.First()
                         select d.Settlement.SettlementName;

            cbTo.DataSource = dataTo.ToList();
            ShowConcreteDetailsBindControls();
        }

        private void cbTo_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShowConcreteDetailsBindControls();
        }

        private void btnRemoveStartHour_Click(object sender, EventArgs e)
        {
            bool confirm =
                MessageBox.Show("Are you sure you want to remove " + cbStartHours.SelectedValue.ToString() +
                "?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes;

            if (confirm)
            {
                DAO.RemoveStartHour(cbStartHours.SelectedValue.ToString());
            }

            StartHoursBindControls();
        }
    }
}
